import { HOST } from '../core/dict';
import logger from '../core/logger';
import { clientBase } from './clientBase';
import ajaxResult from '../models/apis/ajaxResult';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';
import { collectionInfo } from '../models/apis/collectionInfo';
import current from '../core/current';

class collectionClient extends clientBase {
  constructor() {
    super();
  }

  public getAll(userId: string, pageIndex: number): Promise<ajaxResult<Array<collectionInfo>>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/collection/getAll?userId=${userId}&pageIndex=${pageIndex}`)
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: Array<collectionInfo> = response.data.result;
            resolve(this.buildSuccessResult<Array<collectionInfo>>(ret));
          } else
            resolve(this.buildFailedResult<Array<collectionInfo>>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<Array<collectionInfo>>(err));
      });
    });
  }

  public get(userId: string, appId: string): Promise<ajaxResult<number>> {
    return new Promise((resolve) => {
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/collection/get?userId=${userId}&appId=${appId}`, {
        headers: { Authorization: `Bearer ${current.instance.token}` } })
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: number = response.data.result;
            resolve(this.buildSuccessResult<number>(ret));
          } else
            resolve(this.buildFailedResult<number>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<number>(err));
      });
    });
  }

  public addCollection(data: collectionInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.post<string, AxiosResponse<string>, any>(`${HOST}api/collection/add`, data, {
        headers: { Authorization: `Bearer ${current.instance.token}` } })
        .then((response: AxiosResponse) => {
          if (response.status == 200) {
            let ret: string = response.data.result;
            resolve(this.buildSuccessResult<string>(ret));
          } else
            resolve(this.buildFailedResult<string>(response.statusText));
        }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }

  public deleteCollection(data: collectionInfo): Promise<ajaxResult<string>> {
    return new Promise((resolve) => {
      axios.put<string, AxiosResponse<string>, any>(`${HOST}api/collection/delete`, data, {
        headers: { Authorization: `Bearer ${current.instance.token}` }
      }).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: string = response.data.result;
          resolve(this.buildSuccessResult<string>(ret));
        } else
          resolve(this.buildFailedResult<string>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.error(err);
        resolve(this.buildExceptionResult<string>(err));
      });
    });
  }
}
export default new collectionClient();